package krow.dev.gunpobus.view.dialog;

import krow.dev.gunpobus.R;
import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;

public class ConfirmDialog extends Dialog {

	private Context mContext;

	private TextView mTitleView;
	private TextView mContentView;
	private Button mPositiveButton;
	private Button mNegativeButton;
	private View mTitleLayout;
	private View mTopDivierView;
	private View mBottomDividerView;
	
	private boolean mIsPositiveButtonable;
	private boolean mIsNegativeButtonable;
	
	public ConfirmDialog(Context context) {
		super(context);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
		WindowManager.LayoutParams windowParams = getWindow().getAttributes();
		windowParams.dimAmount = 0.7f;
		getWindow().setAttributes(windowParams);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND, WindowManager.LayoutParams.FLAG_DIM_BEHIND);
		setContentView(R.layout.dialog_confirm);

		mContext = context;
		mTitleView = (TextView) findViewById(R.id.text_title);
		mContentView = (TextView) findViewById(R.id.text_content);
		mPositiveButton = (Button) findViewById(R.id.button_ok);
		mNegativeButton = (Button) findViewById(R.id.button_cancel);
	
		mTitleLayout = findViewById(R.id.layout_title);
		mTopDivierView = findViewById(R.id.view_top_divider);
		mBottomDividerView = findViewById(R.id.view_divider);
	}

	@Override
	public void dismiss() {
		super.dismiss();
	}

	public void setTitle(String title) {
		mTitleView.setText(title);
		mTitleLayout.setVisibility(View.VISIBLE);
		mTopDivierView.setVisibility(View.VISIBLE);
	}

	public void setTitle(int resourceId) {
		mTitleView.setText(mContext.getText(resourceId));
		mTitleLayout.setVisibility(View.VISIBLE);
		mTopDivierView.setVisibility(View.VISIBLE);
	}
	
	public void setContent(String content) {
		mContentView.setText(content);
	}

	public void setContent(int resourceId) {
		mContentView.setText(mContext.getText(resourceId));
	}	
	
	public void setPositiveButton(String text, View.OnClickListener clickListener) {
		if(mIsNegativeButtonable){
			mBottomDividerView.setVisibility(View.VISIBLE);
		}
		
		mIsPositiveButtonable = true;
		mPositiveButton.setVisibility(View.VISIBLE);
		mPositiveButton.setOnClickListener(clickListener);
		mPositiveButton.setText(text);
	}

	public void setPositiveButton(int resourceId, View.OnClickListener clickListener) {
		setPositiveButton(mContext.getString(resourceId), clickListener);
	}

	public void setNegativeButton(String text, View.OnClickListener clickListener) {
		if(mIsPositiveButtonable){
			mBottomDividerView.setVisibility(View.VISIBLE);
		}
		
		mIsNegativeButtonable = true;
		mNegativeButton.setVisibility(View.VISIBLE);
		mNegativeButton.setOnClickListener(clickListener);
		mNegativeButton.setText(text);
	}

	public void setNegativeButton(int resourceId, View.OnClickListener clickListener) {
		setNegativeButton(mContext.getString(resourceId), clickListener);
	}
}
